package com.xy.etl.kit;

import com.xy.etl.ds.SQResult;
import com.xy.etl.ds.model.DataSourceConfig;
import com.xy.etl.ibatis.XySQLManager;
import com.xy.etl.kit.EtlDao;
import com.xy.etl.ibatis.core.XySQLResult;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * Dao操作
 */
@Slf4j
public class IbatisDao {

    /**
     * 执行
     *
     * @param sqlId
     * @param paraMap
     * @throws Exception
     */
    public static void run(String sqlId, Map<String, Object> paraMap, DataSourceConfig config) throws Exception {
        // 解析SQL
        XySQLResult vo = XySQLManager.buildSql(sqlId, paraMap);
        if (vo == null) {
            throw new Exception("指定ID" + sqlId + "不存在！");
        }
        EtlDao.run(new SQResult(vo.getSql()).setValue(vo.getVoList()), config);
    }

    /**
     * 查询所有列
     *
     * @param sqlId
     * @param paraMap
     * @return
     * @throws Exception
     */
    public static List<Map<String, Object>> list(String sqlId, Map<String, Object> paraMap, DataSourceConfig config) throws Exception {
        // 解析SQL
        XySQLResult vo = XySQLManager.buildSql(sqlId, paraMap);
        if (vo == null) {
            throw new Exception("指定ID" + sqlId + "不存在！");
        }
        return EtlDao.list(new SQResult(vo.getSql()).setValue(vo.getVoList()), config);
    }
}
